const Router = require('koa-router');
const query = require('../util/mysql');
const wrapper = require('../util/wrapper');
const checkError = require('../util/checkError');

const router = new Router();

router.prefix('/api/system');

module.exports = router;

/**
 * 获取协议
 */
router.get('/protocol', async (ctx, next) => {
  const res = await query('select * from protocol');
  if (checkError(res)) {
    ctx.body = await wrapper(new Error('服务异常.'));
    return false;
  }
  ctx.body = await wrapper(res[0].content);
});

/**
 * 修改协议
 */
router.post('/protocol/edit', async (ctx, next) => {
  const { content } = ctx.request.body;
  if (!content) {
    ctx.body = await wrapper(new Error('参数有误.'));
    return false;
  }
  let res = await query(`update protocol set content='${content}' where id=1`);
  if (checkError(res)) {
    ctx.body = await wrapper(new Error('服务异常.'));
    return false;
  }
  ctx.body = await wrapper('修改成功');
});
